<?php

declare(strict_types=1);
/**
 * This file is part of MoChat.
 * @link     https://mo.chat
 * @document https://mochat.wiki
 * @contact  group@mo.chat
 * @license  https://github.com/mochat-cloud/mochat/blob/master/LICENSE
 */
namespace MoChat\App\User\Action\Dashboard;

use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use MoChat\App\User\Logic\ValidationCodeRegisterLogic;
use MoChat\App\ValidationCode\Contract\ValidationCodeContract;
use MoChat\Framework\Action\AbstractAction;
use MoChat\Framework\Request\ValidateSceneTrait;

/**
 * 用户注册.
 *
 * Class Store.
 * @Controller
 */
class ValidationCodeRegister extends AbstractAction
{
    use ValidateSceneTrait;

    /**
     * @Inject
     * @var ValidationCodeRegisterLogic
     */
    protected $validationCodeRegisterLogic;

    /**
     * @Inject
     * @var ValidationCodeContract
     */
    protected $validationCode;

    /**
     * @RequestMapping(path="/dashboard/user/validationCodeRegister", methods="POST")
     * @return array 返回数组
     */
    public function handle(): array
    {
        // 请求参数
        $params = $this->request->all();

        // 类型验证
        $this->validated($params);

        $params = [
            'name' => $this->request->input('phone') . '的手机用户',
            'phone' => $this->request->input('phone'),
            'gender' => 0,
            'status' => 1,
            'roleId' => 0,
            'captcha' => $this->request->input('captcha'),
            'password' => (string) $this->request->input('password', 123456),
            'is_root' => 1,
        ];
        return $this->validationCodeRegisterLogic->handle($params);
    }

    /**
     * 验证规则.
     */
    protected function rules(): array
    {
        return [
            'phone' => 'required|numeric',
            'captcha' => 'required|numeric',
        ];
    }

    /**
     * 属性替换.
     * @return array|string[] ...
     */
    protected function attributes(): array
    {
        return [
            'phone' => '手机号',
            'captcha' => '验证码',
        ];
    }
}
